//
//  CurrencyConverterView.m
//  ZITHER
//
//  Created by weixin on 2019/1/9.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "CurrencyConverterView.h"
#import "CurrencyConverterTbv.h"

#import "CurrencyConverterHead.h"



@interface CurrencyConverterView()
/** 底部取消Button, 默认标题为"取消" */
//@property (nonatomic, strong) UIView *view1;//举报列表
@property (nonatomic, strong) UIView *dimBackgroundView;/**< 半透明黑色背景 */
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic,strong)CurrencyConverterTbv * tbv;
@property (nonatomic,strong)BaseBtn * cancelBtn;
@property (nonatomic,strong)CurrencyConverterHead * head;



@end

@implementation CurrencyConverterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)setModel:(skuDetailModel *)model{
    _model = model;
    self.tbv.model = model;
    self.head.model = model;
//    NSLog(@"model.officialPrices.count  == %d",model.officialPrices.count);
    [self.tbv mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(model.officialPrices.count*50);
    }];

  
//    [self setNeedsLayout];
}

- (void)show
{
    [self addToKeyWindow];
    [self showAnimationWithCompletion:nil];
}

- (void)hide
{
    [self hideAnimationWithCompletion:^(BOOL finished) {
        [self removeFromKeyWindow];
    }];
}


#pragma mark - private method

- (void)addToKeyWindow
{
    if (!self.superview) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:self];
        
        [self addSubview:self.dimBackgroundView];
        [self addSubview:self.tbv];
        [self addSubview:self.cancelBtn];
        [self addSubview:self.head];
        [self.tbv mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-50);
            make.left.right.equalTo(self).offset(0);
            make.height.mas_equalTo(self.model.officialPrices.count*50);
        }];
        [self.head mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.tbv.mas_top).offset(0);
            make.left.right.equalTo(self).offset(0);
            make.height.mas_equalTo(110);
        }];
    }
}

- (void)removeFromKeyWindow
{
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)showAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0.3;
    } completion:completion];
}

- (void)hideAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0;
    } completion:completion];
}

#pragma mark - getter
- (UIView *)dimBackgroundView
{
    if (!_dimBackgroundView) {
        _dimBackgroundView = [[UIView alloc] init];
        _dimBackgroundView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        _dimBackgroundView.backgroundColor = [UIColor blackColor];
        _dimBackgroundView.alpha = 0;
        
        // 添加手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [_dimBackgroundView addGestureRecognizer:tap];
    }
    return _dimBackgroundView;
}

-(CurrencyConverterTbv *)tbv{
    if (!_tbv) {
        _tbv = [[CurrencyConverterTbv alloc]initializeFrame:CGRectMake(0, 0, kScreenW, 100) isMj:0 isEstimatedRowHeight:YES addRegisterNib:@[@"CurrencyConverterTbvCell"]];

        // 667
    }
    return _tbv;
}

-(BaseBtn *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, kScreenH-50, kScreenW, 50) NormalText:@"关闭" NormalTextColor:[UIColor colorWithHex:@"333333"] SeleText:@"" SeleTextColor:nil font:15 isBold:NO];
        /** 按钮点击案例 */
        @weakify(self);
        [[_cancelBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self hide];
        }];

    }
    return _cancelBtn;
}

-(void)btnChange{
    _head.refreshIconbtn.selected = !_head.refreshIconbtn.selected;
    _head.refreshbtn.selected = !_head.refreshbtn.selected;
    if (_head.refreshIconbtn.selected) {
        self.tbv.state = 1;
    }else{
        self.tbv.state = 0;
    }
    [self.tbv reloadData];
}

-(CurrencyConverterHead *)head{
    if (!_head) {
        _head = [[CurrencyConverterHead alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 110)];
        @weakify(self);
        [[_head.refreshbtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self btnChange];
        }];
        [[_head.refreshIconbtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self btnChange];

        }];
     
    }
    return _head;
}
@end
